/******************************************************************************
	This file is a part of the Snapshot Bioinformatics Project

	Copyright (C) 2012-2013 Jarom Schow

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#include "ConnectFrame.h"

#include <QtWidgets/QGridLayout>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QComboBox>
#include <QtWidgets/QFileDialog>

#include "FileUtil.h"

const QString	c_defaultHost = "127.0.0.1";

const int	c_postgreDefaultPort = 5432;
const int	c_mySqlDefaultPort = 3306;

/**
**/
ConnectFrame::ConnectFrame( QWidget *parent, const Bio::SqlServerSettings &settings ) : QFrame( parent )
{
	int									port = settings.getPort();
	Bio::SqlServerSettings::ServerType	type = settings.getServerType();

	buildUI();

	if( port < 0 ){
		if( type == Bio::SqlServerSettings::PostgreSQL )
			port = c_postgreDefaultPort;
		else if( type == Bio::SqlServerSettings::MySQL )
			port = c_mySqlDefaultPort;
		else
			port = 0;
	}

	m_serverTypeCombo->setCurrentIndex( type );
	m_locationEdit->setText( Bio::FileUtil::getPath( settings.getConfigurationFile() ) );
	m_hostEdit->setText( settings.getHostName() );
	m_portEdit->setText( QString::number( port ) );
	m_userEdit->setText( settings.getUserName() );
	m_passEdit->setText( settings.getPassword() );
	m_dbNameEdit->setText( settings.getDatabaseName() );

	updateServerType( m_serverTypeCombo->currentIndex() );
}

/**
**/
ConnectFrame::~ConnectFrame()
{
}

/**
**/
Bio::SqlServerSettings ConnectFrame::getServerSettings() const
{
	Bio::SqlServerSettings	settings;

	int	idx = m_serverTypeCombo->currentIndex();

	if( idx == 0 )
		settings.setServerType( Bio::SqlServerSettings::MySQLEmbedded );
	else if( idx == 1 )
		settings.setServerType( Bio::SqlServerSettings::MySQL );
	else if( idx == 2 )
		settings.setServerType( Bio::SqlServerSettings::PostgreSQL );

	settings.setHostName( m_hostEdit->text() );
	settings.setPort( m_portEdit->text().toInt() );
	settings.setUserName( m_userEdit->text() );
	settings.setPassword( m_passEdit->text() );
	settings.setDatabaseName( m_dbNameEdit->text() );
	settings.setConfigurationFile( m_locationEdit->text() + "/my.ini" );

	return settings;
}

/**
**/
void ConnectFrame::updateServerType( int index )
{
	if( index == 0 ){
		m_hostLbl->hide();
		m_hostEdit->hide();
		m_portLbl->hide();
		m_portEdit->hide();
		m_userLbl->hide();
		m_userEdit->hide();
		m_passLbl->hide();
		m_passEdit->hide();
		m_dbNameLbl->hide();
		m_dbNameEdit->hide();
		m_locationLbl->show();
		m_locationEdit->show();
		m_locationBtn->show();
	}
	else {
		m_locationLbl->hide();
		m_locationEdit->hide();
		m_locationBtn->hide();
		m_hostLbl->show();
		m_hostEdit->show();
		m_portLbl->show();
		m_portEdit->show();
		m_userLbl->show();
		m_userEdit->show();
		m_passLbl->show();
		m_passEdit->show();
		m_dbNameLbl->show();
		m_dbNameEdit->show();

		if( index == 1 )
			m_portEdit->setText( QString::number( c_mySqlDefaultPort ) );
		else
			m_portEdit->setText( QString::number( c_postgreDefaultPort ) );
	}
}

/**
**/
void ConnectFrame::browseLocation()
{
	QString	newDir = QFileDialog::getExistingDirectory( this, tr( "Select a directory for the database files" ), 
						m_locationEdit->text() );

	if( !newDir.isEmpty() )
		m_locationEdit->setText( newDir );
}

/**
**/
void ConnectFrame::buildUI()
{
	int			row = 0;
	int			col = 0;
	QLabel		*lbl;
	QGridLayout	*layout = new QGridLayout;

	setLayout( layout );

	lbl = new QLabel( tr( "Database Type:" ), this );
	layout->addWidget( lbl, row, col++ );

	m_serverTypeCombo = new QComboBox( this );
	layout->addWidget( m_serverTypeCombo, row, col++, 1, 4 );
	m_serverTypeCombo->addItem( "MySQL Embedded" );
	m_serverTypeCombo->addItem( "MySQL" );
	m_serverTypeCombo->addItem( "PostgreSQL" );
	connect( m_serverTypeCombo, SIGNAL(activated(int)), this, SLOT(updateServerType(int)) );

	row++;
	col = 0;

	m_locationLbl = new QLabel( tr( "Database Location:" ), this );
	layout->addWidget( m_locationLbl, row, col++ );

	m_locationEdit = new QLineEdit( this );
	layout->addWidget( m_locationEdit, row, col, 1, 3 );
	col += 3;

	m_locationBtn = new QPushButton( "...", this );
	m_locationBtn->setFixedWidth( 30 );
	connect( m_locationBtn, SIGNAL(clicked()), this, SLOT(browseLocation()) );
	layout->addWidget( m_locationBtn, row, col++ );

	row++;
	col = 0;

	m_hostLbl = new QLabel( tr( "Host:" ), this );
	layout->addWidget( m_hostLbl, row, col++ );

	m_hostEdit = new QLineEdit( this );
	layout->addWidget( m_hostEdit, row, col++ );

	m_portLbl = new QLabel( tr( "Port:" ), this );
	layout->addWidget( m_portLbl, row, col++ );

	m_portEdit = new QLineEdit( this );
	layout->addWidget( m_portEdit, row, col, 1, 2 );

	row++;
	col= 0;

	m_userLbl = new QLabel( tr( "User:" ), this );
	layout->addWidget( m_userLbl, row, col++ );

	m_userEdit = new QLineEdit( this );
	layout->addWidget( m_userEdit, row, col, 1, 4 );

	row++;
	col = 0;

	m_passLbl = new QLabel( tr( "Password:" ), this );
	layout->addWidget( m_passLbl, row, col++ );

	m_passEdit = new QLineEdit( this );
	m_passEdit->setEchoMode( QLineEdit::Password );
	layout->addWidget( m_passEdit, row, col, 1, 4 );

	row++;
	col = 0;

	m_dbNameLbl = new QLabel( tr( "Database Name:" ), this );
	layout->addWidget( m_dbNameLbl, row, col++ );

	m_dbNameEdit = new QLineEdit( this );
	layout->addWidget( m_dbNameEdit, row, col, 1, 4 );

	row++;

	layout->setColumnStretch( 1, 100 );
	layout->setRowStretch( row, 100 );

	setOnlineHelp();
}

/**
**/
void ConnectFrame::setOnlineHelp()
{
	m_serverTypeCombo->setToolTip( "Select the type of server you want to connect to." );
	m_serverTypeCombo->setWhatsThis( "Select the type of server you want to connect to. "
		"Snapshot currently supports PostgreSQL and MySQL servers." );

	m_hostEdit->setToolTip( "The IP address or host name of the server." );
	m_hostEdit->setWhatsThis( "Enter the host name or IP address of the server. "
		"If the server is running on the same computer as Snapshot, use 127.0.0.1 "
		"as the host name. If the server is on another computer, typically you will "
		"enter it's IP address. Check with your database administrator if you are unsure "
		"what the value should be." );

	m_portEdit->setToolTip( "The port that the server is listening on." );
	m_portEdit->setWhatsThis( "Enter the port that the server is listening on. "
		"For MySQL, the default is 3306. For PostgreSQL, the default is 5432. "
		"Check with your database administrator if you are unsure what the value should be." );

	m_userEdit->setToolTip( "Enter the user name to use to connect to the database." );
	m_userEdit->setWhatsThis( "Enter the user name to use to connect to the database. "
		"This is the user name that will be used to log on to the database server. At a "
		"minimum, the user must have read access for the BioSQL database." );

	m_passEdit->setToolTip( "Enter the password for the user account." );
	m_passEdit->setWhatsThis( "Enter the password for the user account. This is the "
		"password for the user account that will be used to log onto the database server." );

	m_dbNameEdit->setToolTip( "Enter the name of the BioSQL database." );
	m_dbNameEdit->setWhatsThis( "Enter the name of the BioSQL database. This is the name of the "
		"BioSQL database on the database server." );
}
